package com.example.demo.four;

import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;

public abstract class Message implements Runnable {

    protected String name;//参赛选手

    protected int speed;//参赛选手的速度

    protected int now;//当前线程已跑的路程

    protected ExecutorService executor;//线程管理

    protected CyclicBarrier barrier;//等待计算器，模拟点击

    public static volatile boolean FINISH = false;//是否完成比赛的完成标志

    public final static int SUCCESS = 200;//比赛的路程

    public Message(String name, int speed, ExecutorService executor, CyclicBarrier barrier) {
        this.name = name;
        this.speed = speed;
        this.executor = executor;
        this.barrier = barrier;
    }

    public Message(String name, int speed, ExecutorService executor) {
        this.name = name;
        this.speed = speed;
        this.executor = executor;
    }

    @Override
    public abstract void run();

    protected void finish() {
        if (now >= SUCCESS) {
            System.out.println(name + "获得第一名，蝶泳比赛结束");
            FINISH = true;
            executor.shutdownNow();
        }
    }
}
